<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {

	//登录
    public function Login(){

        if(isset($_COOKIE['admin'])){
           $this->success('之前已经登录',U('Index/index'),3);
           exit;
        }

    	if(IS_AJAX){
    		//获取post过来的对应的文本框名称
    		$getInputId = array_keys($_POST)[0];

    		//如果得到是验证码的话
    		if($getInputId == 'verify'){
    			$Verify = new \Think\Verify();
    			$Verify->reset = false; //将reset设置为false 解决只能验证一次的问题
                //检测验证码
    			if( $Verify->check($_POST[$getInputId])){
	    			$data = array(
		    			'error'		 => 0,
		    			'returnMsg'  => '验证码正确',
		    			'message'    => $_POST[$getInputId],
					);
	    		}else{
	    			$data = array(
		    			'error'		 => 1,
		    			'returnMsg'  => '验证码错误',
		    			'message'    => $_POST[$getInputId],
					);
	    		}
    		//如果是用户名
    		}elseif ($getInputId == 'adminname') {
    			$adminname = $_POST[$getInputId];
    			$table = M('admin');
    			$sql = "select * from admin where admin_name='{$adminname}'";
    			$num = count($table->query($sql));
    			if($num){
    				$_SESSION['adminname'] = $adminname;  //保存管理员名字做登录
    				$data = array(
		    			'error'		 => 0,
		    			'returnMsg'  => '正确',
		    			'message'    => $getInputId,
					);
    			}else{
    				$data = array(
		    			'error'		 => 1,
		    			'returnMsg'  => '错误',
		    			'message'    => $getInputId,
					);
					if(isset($_SESSION['adminname'])){
						unset($_SESSION['adminname']);    //销毁管理员session
					}
    			}

			//如果是密码
    		}elseif($getInputId == 'adminpwd' && isset($_SESSION['adminname'])){
    			$adminpwd = $_POST[$getInputId];
    			$table = M('admin');
    			$sql = "select * from admin where admin_name='{$_SESSION['adminname']}' and admin_password = '{$adminpwd}' ";
    			$num = count($table->query($sql));
    			if($num){
    				$data = array(
		    			'error'		 => 0,
		    			'returnMsg'  => '正确',
		    			'message'    => $getInputId,
					);
    			}else{
    				$data = array(
		    			'error'		 => 1,
		    			'returnMsg'  => '错误',
		    			'message'    => $getInputId,
					);
    			}
    		//如果说没有设置SESSION['adminname'] 和 得到的不是 'adminpwd' 的话 返回的json信息为空
    		}else{
    			$data = array(
	    			'error'		 => '',
	    			'returnMsg'  => '',
	    			'message'    => $getInputId,
				);
    		}
    		
        	$this->ajaxReturn($data);
        }elseif(!IS_AJAX && $_POST){
            $adminname = $_POST['adminname'];
            $adminpassword = $_POST['adminpwd'];
            $sql = "select * from admin where admin_name = '{$adminname}' and admin_password = '{$adminpassword}'";
            $table = M('admin');
            $result = $table->query($sql);
            if($result){
                //判断是否有选择保持登录状态的选项
                if(isset($_POST['online'])){
                   // setcookie($adminname,$adminname,time()+3600*24*30,'/');
                    setcookie($adminname,$adminname,time()+15,'/');
                }else{
                    unset($_COOKIE[$adminname]); //如果的话就清空cookie;
                }
                $_SESSION['adminname'] = $_POST['adminname'];
                $_SESSION['adminid'] = $result['0']['admin_id'];
                $this->success('登录成功',U('admin/index/index'),3);
                exit;
            }else{
                $this->error('登录失败',U('Login/login'));
                exit;
            }
            
        }
        $this->display();
    }

    public function buildVerify(){
    	$Verify = new \Think\Verify();
    	$Verify->fontSize = 50;
    	$Verify->entry();
    }

    public function loginOut(){
        session(null);
        $this->success('退出成功',U('admin/login/login'));
    }

}